package com.example.computer_exam_training_sys.service.impl;

import com.example.computer_exam_training_sys.entity.DiscussionTopicInfo;
import com.example.computer_exam_training_sys.mapper.DiscussionTopicMapper;
import com.example.computer_exam_training_sys.service.DiscussionTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

@Service
public class DiscussionTopicServiceImpl implements DiscussionTopicService {

    @Autowired
    private DiscussionTopicMapper topicMapper;

    @Override
    public int addTopic(DiscussionTopicInfo topic) {
        return topicMapper.insertTopic(topic);
    }

    @Override
    public int deleteTopic(Integer topicId) {
        int result = topicMapper.deleteTopicById(topicId); // 调用 Mapper 的删除方法
        return result > 0 ? 1 : 0; // 如果删除成功返回1，否则返回0
    }

    @Override
    public DiscussionTopicInfo getTopicById(Integer topicId) {
        return topicMapper.getTopicById(topicId);
    }

    @Override
    public List<DiscussionTopicInfo> getTopicList() {
        return topicMapper.getTopicList();
    }

    @Override
    public List<DiscussionTopicInfo> getTopicListByPage(Integer topicId, int page, int size) {
        int offset = (page - 1) * size;
        return topicMapper.getTopicListByPage(topicId, offset, size);
    }

    @Override
    public int countTopics(Integer topicId) {
        return topicMapper.countTopics(topicId);
    }

    @Override
    public List<DiscussionTopicInfo> getTopicWithComments(Integer topicId) {
        return topicMapper.getTopicWithComments(topicId);
    }

    @Override
    public List<Map<String, Object>> getTopicWithCommentsDetails(Integer topicId) {
        return topicMapper.getTopicWithCommentsDetails(topicId);
    }

    @Override
    public List<DiscussionTopicInfo> searchTopicsByTopicIdOrTitle(Integer topicId, String title, int page, int size) {
        int offset = (page - 1) * size;
        return topicMapper.searchTopicsByTopicIdOrTitle(topicId, title, offset, size);
    }

    @Override
    public int countSearchTopics(Integer topicId, String title) {
        return topicMapper.countSearchTopics(topicId, title);
    }

    @Override
    public List<DiscussionTopicInfo> getTopicListWithCourseName(Integer topicId, String courseName, String keyword, int page, int size) {
        int offset = (page - 1) * size;
        return topicMapper.getTopicListWithCourseName(topicId, courseName, keyword, offset, size);
    }

    @Override
    public int countTopicsWithCourseName(Integer topicId, String courseName, String keyword) {
        return topicMapper.countTopicsWithCourseName(topicId, courseName, keyword);
    }

    @Override
    public List<Map<String, Object>> getTopCourses() {
        return topicMapper.getTopCourses();
    }

    @Override
    public List<Map<String, Object>> getHotTopics() {
        return topicMapper.getHotTopics();
    }

    // DiscussionTopicServiceImpl.java
    @Override
    public int increaseViewCount(Integer topicId) {
        return topicMapper.increaseViewCount(topicId);
    }
}